package com.freesid.ask.client;

import java.util.LinkedHashMap;
import java.util.List;

import com.freesid.ask.client.field.BaseField;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ModifyPanel extends Composite {

	private VerticalPanel container = new VerticalPanel();

	private HorizontalPanel header = new HorizontalPanel();

	private HorizontalPanel footer = new HorizontalPanel();

	private VerticalPanel center = new VerticalPanel();

	String mainDivId = "";

	public ModifyPanel(String mainDivId) {

		this.mainDivId = mainDivId;
		container.add(header);
		container.add(center);
		container.add(footer);

		container.setStyleName("editPanel");
		// container.setWidth("200px");

		// center.add(new HTML("fffffffff"));

		List<String> fns = FieldFactory.getFactory().getAllFieldNames();

		for (int i = 0; i < fns.size(); i++) {

			Button btn = FieldFactory.getFactory().getFieldButtonByName(fns.get(i));

			btn.addClickHandler(new MenuClickHandler(fns.get(i)));

			center.add(btn);
		}

		initWidget(container);
	}

	private LinkedHashMap<Integer, BaseField> fieldMap = new LinkedHashMap<Integer, BaseField>();

	private Integer number = 0;

	private class MenuClickHandler implements ClickHandler {

		String fieldName;

		public MenuClickHandler(String fieldName) {
			this.fieldName = fieldName;
		}

		@Override
		public void onClick(ClickEvent event) {
			BaseField field = FieldFactory.getFactory().getFieldByName(fieldName);
			Integer n = number++;
			field.setId(n);
			field.setOrder(n);
			
			field.getNumberLabel().setText(n+"." + fieldName) ;
			
			field.getTitleLabel().setText("please modify the title. click the edit button.");

			field.getDeleteBtn().addClickHandler(new DeleteButtonClickHandler(field));
			
			field.getDownBtn().addClickHandler(new DownButtonClickHandler(field));

			field.getUpBtn().addClickHandler(new UpButtonClickHandler(field));

			field.getTopBtn().addClickHandler(new TopButtonClickHandler(field));

			field.getBottomBtn().addClickHandler(new ButtomButtonClickHandler(field));
			
			field.getEditBtn().addClickHandler(new EditButtonClickHandler(field));

			RootPanel.get(mainDivId).add(field);

			fieldMap.put(n, field);
			
			foucsField(n);
			
			Window.scrollTo(0, field.getContentTable().getAbsoluteTop() );
//			field.getEditBtn().click();//default click
		}

	}
	
	protected void foucsField(Integer id) {
		for (Integer key : fieldMap.keySet()) {
			fieldMap.get(key).getContainer().removeStyleName("editing");
		}
		fieldMap.get(id).getContainer().addStyleName("editing");
	}

	private abstract class BaseButtonClickHandler implements ClickHandler {

		protected BaseField field;

		public BaseButtonClickHandler(BaseField field) {
			this.field = field;
		}

	}

	private class DeleteButtonClickHandler extends BaseButtonClickHandler {

		public DeleteButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			
			field.removeFromParent();
			fieldMap.remove(field.getId());
		}

	}

	private class DownButtonClickHandler extends BaseButtonClickHandler {

		public DownButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			foucsField(field.getId());
			int count = RootPanel.get(mainDivId).getWidgetCount();
			int index = RootPanel.get(mainDivId).getWidgetIndex(field);
			if (count - 1 == index) {
				return;
			}

			BaseField next = (BaseField) RootPanel.get(mainDivId).getWidget(index + 1);
			RootPanel.get(mainDivId).insert(next, index);

			int temp = field.getOrder();
			field.setOrder(next.getOrder());
			next.setOrder(temp);
		}

	}

	private class UpButtonClickHandler extends BaseButtonClickHandler {

		public UpButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			foucsField(field.getId());
			// int count = RootPanel.get(mainDivId).getWidgetCount() ;
			int index = RootPanel.get(mainDivId).getWidgetIndex(field);
			if (index == 0) {
				return;
			}

			BaseField pre = (BaseField) RootPanel.get(mainDivId).getWidget(index - 1);
			RootPanel.get(mainDivId).insert(field, index - 1);

			int temp = field.getOrder();
			field.setOrder(pre.getOrder());
			pre.setOrder(temp);
		}

	}

	private class TopButtonClickHandler extends BaseButtonClickHandler {

		public TopButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			foucsField(field.getId());
			int index = RootPanel.get(mainDivId).getWidgetIndex(field);
			if (index == 0) {
				return;
			}
			
			RootPanel.get(mainDivId).insert(field, 0);

			BaseField next = (BaseField) RootPanel.get(mainDivId).getWidget(0);
			int temp = field.getOrder();
			field.setOrder(next.getOrder());
			next.setOrder(temp);
		}

	}

	private class ButtomButtonClickHandler extends BaseButtonClickHandler {

		public ButtomButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			foucsField(field.getId());
			int count = RootPanel.get(mainDivId).getWidgetCount();
			int index = RootPanel.get(mainDivId).getWidgetIndex(field);
			if (count - 1 == index) {
				return;
			}

			RootPanel.get(mainDivId).add(field);

			BaseField bot = (BaseField) RootPanel.get(mainDivId).getWidget(count - 1);
			int temp = field.getOrder();
			field.setOrder(bot.getOrder());
			bot.setOrder(temp);

		}

	}
	
	private class EditButtonClickHandler extends BaseButtonClickHandler {

		public EditButtonClickHandler(BaseField field) {
			super(field);
		}

		@Override
		public void onClick(ClickEvent event) {
			
//			Window.alert(field.getXml().toString());
			
			foucsField(field.getId());
			DialogBox box = field.getEditDialogBox();
			
//			box.setText(field.getId()+"");
			field.setEditDialogBoxTitle(box);
			box.setAnimationEnabled(true);
			box.center();
			box.show();
			
		}
		
	}

}
